require 'rexml/document'

module StreamiumTestHelper
  
  attr :resp
  
  def send_request(msg)
    @resp.pop
  end
  
  def create_response(totnumelems, startid, numelems)
    @resp.push(create_content_data_set(totnumelems, startid, numelems))
  end
  
    
  def create_content_data(content = {})
    node = ContentData.new(create_content_data_node(content))
    if node.parentid
      @children = {} if !@children
      @children[node.parentid] ||= []
      @children[node.parentid] << node
    end
    node
  end 

  def create_content_data_node(content = {})
    doc = REXML::Document.new
    root = doc.add_element("contentdata")
    content.each_key do |k|
      root.add_element(k.to_s).text = content[k].to_s
    end
    root
  end

  
  def create_content_data_set(totnumelems = 5, startid = 0, numelems = 5)
    doc = Document.new
    set = doc.add_element("contentdataset")
    (1..numelems).each do |i|
      id = startid + i
      data = set.add_element("contentdata")
      data.add_element("name").text = "Data Node #{id}"
      data.add_element("nodeid").text = "#{id}"
      data.add_element("parentid").text = "0"
    end
    set.add_element("totnumelem").text = totnumelems
    set.add_element("numelem").text = totnumelems <= numelems ? totnumelems : numelems
    set.add_element("fromindex").text = startid
    doc.to_s
  end
end
